package com.carSys.Service;

import com.carSys.Entity.Assignment;
import com.carSys.Entity.MaterialUsage;
import com.carSys.Entity.RepairOrder;
import com.carSys.Enums.AssignmentStatus;
import com.carSys.Mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class AssignmentService {

    @Autowired
    private AssignmentMapper assignmentMapper;

    @Autowired
    private RepairOrderMapper repairOrderMapper;

    @Autowired
    private RepairPersonMapper repairPersonMapper;

    @Autowired
    private PartMapper partMapper;

    @Autowired
    private MaterialUsageMapper materialUsageMapper;

    public List<Assignment> getAssignmentsByRepairPersonIdAndStatus(Long id, AssignmentStatus status) {
        return assignmentMapper.getAssignmentsByRepairPersonIdAndStatus(id, status);
    }

    // 生成维修任务
    public boolean insertAssignment(Assignment assignment) {
        return assignmentMapper.insertAssignment(assignment) > 0;
    }

    // 分配维修任务
    public boolean distributeAssignment(long assignment_id,long repairPersonId) {
        return assignmentMapper.distributeAssignment(assignment_id,repairPersonId) > 0;
    }

    // 拒绝维修任务
    public boolean refuseAssignment(long assignment_id,long repairPersonId) {
        return assignmentMapper.refuseAssignment(assignment_id,repairPersonId) > 0;
    }

    public String refusedRepairPersonsByAssignmentId(long assignment_id) {
        return assignmentMapper.getRefusedRepairPersonsByAssignmentId(assignment_id);
    }

    // 接受维修任务
    public boolean acceptAssignment(long assignment_id,long repairPersonId) {
        return assignmentMapper.acceptAssignment(assignment_id,repairPersonId) > 0;
    }

    //完成维修任务
    @Transactional(rollbackFor = Exception.class)
    public boolean completeAssignment(long assignment_id) {
        Assignment assignment = assignmentMapper.getAssignmentById(assignment_id);
        if (assignment == null) {
            return false; // 任务不存在
        }

        // 将当前任务设置为已完成
        boolean ret = assignmentMapper.completeAssignment(assignment_id) > 0;

        long order_id = assignment.getOrder_id();
        RepairOrder order = repairOrderMapper.selectRepairOrderById(order_id);
        double totalCost = order.getTotal_cost();
        // 加上时薪
        long repair_person_id = assignment.getRepair_person_id();
        double hourly_rate = repairPersonMapper.selectRepairPersonById(repair_person_id).getHourly_rate();
        double timeCost = assignment.getHours_worked() * hourly_rate;
        assignmentMapper.setTimeCost(assignment_id, timeCost);
        totalCost += timeCost;
        // 加上材料费用
        List<MaterialUsage> materialUsages = materialUsageMapper.selectMaterialUsageByAssignmentId(assignment_id);
        for (MaterialUsage usage : materialUsages) {
            long part_id = usage.getPart_id();
            int quantity = usage.getQuantity();
            double partCost = partMapper.selectPartById(part_id).getUnit_price();
            totalCost += partCost * quantity;

            // 更新材料库存
            if (!partMapper.reduceStock(part_id, quantity)) {
                throw new RuntimeException("库存不足，无法完成任务");
            }
        }
        // 更新费用
        repairOrderMapper.updateRepairOrderCostById(order_id, totalCost);

        // 检查还有没有未完成的任务
        List<Assignment> assignments = assignmentMapper.selectAssignmentsByOrderId(order_id);
        for (Assignment assign : assignments) {
            if (assign.getStatus() != AssignmentStatus.COMPLETED) {
                return ret; // 还有未完成的任务
            }
        }

        return ret;
    }

    // 查询订单对应的分配记录
    public List<Assignment> getAssignmentsByOrderId(long order_id) {
        return assignmentMapper.selectAssignmentsByOrderId(order_id);
    }

    // 查询维修人员对应的分配记录
    public List<Assignment> getAssignmentsByRepairPersonId(long repair_person_id) {
        return assignmentMapper.selectAssignmentsByRepairPersonId(repair_person_id);
    }

    // 更新分配状态
    public int updateAssignmentStatus(Assignment assignment) {
        return assignmentMapper.updateAssignmentStatus(assignment);
    }

    public Assignment getAssignmentById(long assignment_id) {
        return assignmentMapper.getAssignmentById(assignment_id);
    }

    public boolean logTime(long id, int time) {
        return assignmentMapper.logTime(id, time) > 0;
    }

    public boolean commentAssignment(long assignment_id, int score) {
        if(assignmentMapper.getAssignmentById(assignment_id).getStatus()!= AssignmentStatus.COMPLETED) {
            return false; // 任务未完成，不能评价
        }
        return assignmentMapper.commentAssignment(assignment_id, score) > 0;
    }

    public List<Assignment> getAllAssignments() {
        return assignmentMapper.selectAllAssignments();
    }
}